package com.arong.swing.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.sql.Connection;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.arong.swing.db.JDBCUtils;
import com.arong.swing.db.dao.CrCheckeventSDAO;
import com.arong.swing.db.dao.IBatchDeal;
import com.arong.swing.db.dao.KeyPersonSDAO;
import com.arong.swing.db.entity.CrCheckevent;
import com.arong.swing.db.entity.KeyPerson;
import com.arong.swing.exception.AppException;
import com.arong.swing.exception.SysException;

public class CKDataFile {
	private String filePath = "anrongCK.dat";
	
	public CKDataFile(String filePath){
		this.filePath = filePath;
	}
	
	public List<String[]> loadDataFromFile(){
		FileChannel fc = null;
		int loaded = 0;
		File f = null;
		List<String[]> list = new ArrayList<String[]>();
		try{
			f = new File(filePath);
			if(f.exists() == false){
				return list;
			}
			fc = new FileInputStream(f).getChannel();
			byte[] entry = new byte[2048];
			ByteBuffer bb = ByteBuffer.wrap(entry);

			int size = readInt(fc, bb);//总数
			int columnsSize= readInt(fc, bb);//列数
			if(size > 0){
				byte[] da0x20 = null;
				for(int i = 0; i < size; i++){
					String[] s = new String[columnsSize];
					for(int j = 0 ; j < columnsSize;j++){
						int daLen = readInt(fc, bb);
						da0x20 = readByte(fc, bb,daLen);
						s[j]=new String(da0x20);
					}
					
					list.add(s);
					loaded++;
				}
			}
			System.out.println(loaded+" loaded ");
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(fc != null){
				try{
					fc.close();
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}
		return list;
	}
	private static void bufferRead(FileChannel fc, ByteBuffer bb) throws IOException {
		while(bb.remaining() >0){
			if(fc.read(bb) < 0){
				break;
			}
		}
	}
	private static int readInt(FileChannel fc, ByteBuffer bb) throws IOException {
		bb.clear();
		bb.limit(4);
		bufferRead(fc, bb);
		bb.flip();
		int daLen = bb.getInt();
		return daLen;
	}
	private static byte[] readByte(FileChannel fc, ByteBuffer bb,int length) throws IOException {
		bb.clear();
		byte [] da0x20 = new byte[length];
		bb.limit(length);
		bufferRead(fc, bb);
		bb.flip();
		bb.get(da0x20);
		return da0x20;
	}
	public void saveDataToFile(List<String[]>  datas,int columnsSize) throws Exception{
		FileChannel fc = null;
		int savedCount =0 ;
		try{
			File f = new File(filePath);
			if(f.exists() == false){
				f.createNewFile();
			}
			fc = new FileOutputStream(f, false).getChannel();
			ByteBuffer bb = ByteBuffer.allocate(2048);
			
			//数据前缀混淆
			bb.putInt(datas.size());
			bb.putInt(columnsSize);
			bb.flip();
			fc.write(bb);
			
			for(String[] ds : datas){
				for(String data :ds){
					bb.clear();
					bb.putInt(data.getBytes("UTF-8").length);
					bb.put(data.getBytes("UTF-8"));
					bb.flip();
					fc.write(bb);
				}
				savedCount++;
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(fc != null){
				System.out.println("saved "+savedCount +" to anrongCK.dat");
				try{
					fc.close();
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}
	}
	public static void saveDataToFile() throws Exception{
		FileChannel fc = null;
		int savedCount =0 ;
		try{
			File f = new File("E:\\temp\\anrongCK.dat");
			if(f.exists() == false){
				f.createNewFile();
			}
			fc = new FileOutputStream(f, false).getChannel();
			ByteBuffer bb = ByteBuffer.allocate(2048);
			
			//数据前缀混淆
			bb.putInt(6000000);
			bb.putInt(5);
			bb.flip();
			fc.write(bb);
			
			for(int i =0 ; i< 6000000 ;i++){
				String[] ds =new String[]{""+i,"中文 nini","nig ","130228918033433","各个 55 eee"};
				for(String data :ds){
					bb.clear();
					bb.putInt(data.getBytes("UTF-8").length);
					bb.put(data.getBytes("UTF-8"));
					bb.flip();
					fc.write(bb);
				}
				savedCount++;
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(fc != null){
				System.out.println("saved "+savedCount +" to anrongCK.dat");
				try{
					fc.close();
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}
	}
	public static void readDataFromFile(){
		FileChannel fc = null;
		int loaded = 0;
		File f = null;
		try{
			f = new File("E:\\temp\\anrongCK.dat");
			if(f.exists() == false){
				return ;
			}
			fc = new FileInputStream(f).getChannel();
			byte[] entry = new byte[2048];
			ByteBuffer bb = ByteBuffer.wrap(entry);

			int size = readInt(fc, bb);//总数
			int columnsSize= readInt(fc, bb);//列数
			if(size > 0){
				byte[] da0x20 = null;
				for(int i = 0; i < size; i++){
					//String[] s = new String[columnsSize];
					for(int j = 0 ; j < columnsSize;j++){
						int daLen = readInt(fc, bb);
						da0x20 = readByte(fc, bb,daLen);
						if(j==0){
							String s=new String(da0x20);
						}
						
					}
					
					loaded++;
				}
			}
			System.out.println(loaded+" loaded ");
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(fc != null){
				try{
					fc.close();
				}catch(Exception e){
					e.printStackTrace();
				}
			}
		}
	}
	public static void main(String[] args) throws Exception {
		//main3(args);
	//	main1(args);
		
		
		
		
		/*long now = System.currentTimeMillis();
		//saveDataToFile();
		readDataFromFile();
		System.out.println("use time: "+(System.currentTimeMillis()-now)/1000+"s.");*/
	/*	
		long now = System.currentTimeMillis();
		KeyPersonSDAO sDao = new KeyPersonSDAO("E:\\temp\\anrongCK.dat");
		KeyPerson key =sDao.queryBean(1+"000000000088888");
		final JDBCUtils jdbcUtils = new JDBCUtils("E:\\temp\\anrongCK.dat");
		CrCheckeventSDAO dao =new CrCheckeventSDAO(jdbcUtils);
		CrCheckevent query =new CrCheckevent();
		query.setId("2ccd6bc9acf14e7cae870eeae9cf0102");
		CrCheckevent entity = dao.queryBean(query);
		System.out.println(entity.getId());
		System.out.println("use time: "+(System.currentTimeMillis()-now)/1000+"s.");*/
	}
	public static void main1(String[] args) {
		CKDataFile s  =new CKDataFile("anrongCK.dat");
		List<String[]>  datas = new ArrayList<String[]>();
		datas.add(new String[]{"130228918033433","中文 nini","nig ","","各个 55 eee"});
		datas.add(new String[]{"132228918033433","中文 nini","nig ","","各个 55 eee"});
		datas.add(new String[]{"134228918033433","中文 nini","nig ","ggg你好","各个 55 eee"});
		
		try {
			s.saveDataToFile(datas,5);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		datas = s.loadDataFromFile();
		
		for(String[] data : datas){
			for(String str :data){
				System.out.print(str+"-");
			}
			System.out.println();
		}
		
	}
	
	
	public static void main3(String[] args) throws Exception {
		//sqlite10000000测试
		long start = System.currentTimeMillis();
		
		final JDBCUtils jdbcUtils = new JDBCUtils("E:\\temp\\anrongCK.dat");
		CrCheckeventSDAO dao =new CrCheckeventSDAO(jdbcUtils);
		
	/*	long num = jdbcUtils.queryNumValue("select count(*) from CR_CHECKEVENT");
		System.out.println(num);*/
		
	/*	CrCheckevent queryEntity =new CrCheckevent();
		queryEntity.setId("4444444");
		queryEntity = dao.queryBean(queryEntity);*/
		
		
		//** 批量事务操作 *//*
		IBatchDeal batchDeal  =new IBatchDeal() {
			@Override
			public int doBatchDeal(Connection conn) throws Exception {
				for(int i=0 ; i < 10000000; i++){
					CrCheckevent entity = new CrCheckevent();
					entity.setId(UUIDUtils.uuid32());
					entity.setEquiptId("444444");
					entity.setAddres("xxxxxxxx");
					entity.setEndTime(new Timestamp(System.currentTimeMillis()));
					jdbcUtils.insert(conn, entity);
					
					if(i%10000 == 0){
						conn.commit();
						System.out.println(i);
					}
				}
				return 0;
			}
			
		};
		dao.batchCommit(batchDeal);
		
		
		System.out.println("over:"+(System.currentTimeMillis()-start));
	}
	
	public static void main4(String[] args) throws SysException, AppException {
		KeyPersonSDAO sDao = new KeyPersonSDAO("E:\\temp\\persion.data");
		KeyPerson person = null;
		long now = System.currentTimeMillis();
		for(int i = 0; i < 6000000; i++){
			person = new KeyPerson();
			person.setSfzh(i+"000000000088888");
			person.setRylb("涉毒");
			person.setClfs("eggggggggggg");
			person.setAqms("eggggggddddddddddggggg");
			person.setBklxr("eggggggggggg");
			person.setBklxfs("eggggggggggg");
			person.setRwmc("eggggggggggg");
			person.setRwid("eggggggggggg");
			person.setPfirstid("eggggggggggg");
			person.setPverid("eggggggggggg");
			
				sDao.insert(person);
			if(i%5000 ==0){
				System.out.println(i+" use time: "+(System.currentTimeMillis()-now)/1000+"s.");
			}
		}
		System.out.println("use time: "+(System.currentTimeMillis()-now)/1000+"s.");
	}
}
